package br.cin.stadium.middleware.directory;

import br.cin.stadium.middleware.FormatException;
import br.cin.stadium.middleware.marshall.MarshallInputStream;
import br.cin.stadium.middleware.marshall.MarshallOutputStream;
import br.cin.stadium.middleware.marshall.Serializable;

public class ServerLocation implements Serializable {

	private String ip = null;
	private String port = null;

	public ServerLocation(String ip, String port) {
		this.ip = ip;
		this.port = port;
	}

	public ServerLocation(String ip, int port) {
		this.ip = ip;
		this.port = Integer.toString(port);
	}

	public ServerLocation(String ipPort) {
		int charSeparator = ipPort.indexOf(':');

		this.ip = ipPort.substring(0, charSeparator);
		this.port = ipPort.substring(charSeparator + 1);
	}

	public ServerLocation() {
	}

	public boolean equals(Object o) {
		boolean equals = false;

		if (this.ip.equals(((ServerLocation) o).getIp())
				&& (this.port.equals(((ServerLocation) o).getPort()))) {
			equals = true;
		}

		return equals;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public void marshal(MarshallOutputStream mos) throws FormatException {
		mos.writeUTF(this.getIp());
		mos.writeUTF(this.getPort());
	}

	public void unmarshal(MarshallInputStream mis) throws FormatException {
		this.setIp(mis.readUTF());
		this.setPort(mis.readUTF());
	}
	
	public String toString() {
		return "===> " + this.getIp() + ":" + this.getPort();
	}

}
